using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for DropDownListHelper
/// </summary>
public class DropDownListHelper
{

    public void FillDataTableToDropDownList(
    DataTable dataTable, 
    DropDownList dropDownList,
    string displayMember,
    string valueMember,
    string selectedValue)
    {        
        if (dataTable != null)
        {
            dropDownList.DataSource = dataTable;
            dropDownList.DataTextField = displayMember;
            dropDownList.DataValueField = valueMember;
        }
        else
            dropDownList.DataSource = null; 
        dropDownList.DataBind();
        if (selectedValue != "")
            dropDownList.SelectedValue = selectedValue;        
    }

    public void FillDataTableToDropDownList(
    DataSet dataSet, 
    DropDownList[] dropDownList,
    string[] displayMember,
    string[] valueMember, 
    string[] selectedValue)
    {        
        if (dataSet != null)
        {
            for(int i = 0; i < displayMember.Length; i++)
            {               
                dropDownList[i].DataSource = dataSet.Tables[i];
                dropDownList[i].DataTextField = displayMember[i];
                dropDownList[i].DataValueField = valueMember[i];
            }             
        }
        else
        {
            for (int i = 0; i < displayMember.Length; i++)
            {
                dropDownList[i].DataSource = null;
            }          
        }
        for (int i = 0; i < displayMember.Length; i++)
        {
            dropDownList[i].DataBind();
            if (selectedValue[i] != "")
                dropDownList[i].SelectedValue = selectedValue[i];
        }                  
        

    }       
    
    public void FillDataTableToDropDownList(
    DataTable dataTable,
    DropDownList dropDownList,
    string firstDisplayMember,
    string firstValueMember,
    string displayMember,
    string valueMember,
    string selectedValue)
    {
        
        if (dataTable != null)
        {
            if (firstDisplayMember != "")
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow[0] = firstDisplayMember;
                dataRow[1] = firstValueMember;
                dataTable.Rows.InsertAt(dataRow, 0);
            }
            dropDownList.DataTextField = displayMember;
            dropDownList.DataValueField = valueMember;
            dropDownList.DataSource = dataTable;
            dropDownList.DataBind();
            if (selectedValue != "")
                dropDownList.SelectedValue = selectedValue;
        }
        else
        {
            dropDownList.Items.Add(
            new ListItem(firstDisplayMember,
            firstValueMember));
        }


    }
    
    
    
    void FillStringToDropDownList(
    string[] valueCollection,
    DropDownList dropDownList,
    string firstDisplayMember,
    string firstValueMember)
    {
        if (firstDisplayMember != "")
        {
            dropDownList.Items.Add(
            new ListItem(firstDisplayMember,
            firstValueMember));
        }
                
        for (int i = 0; i < valueCollection.Length; i++)
        {
            dropDownList.Items.Add(
            valueCollection[i]);
        }

    }    
}
